package com.glasses.business.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.activity.OrderCompanyActivity;
import com.glasses.business.activity.PayActivity;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.base.AppBaseFragment;
import com.glasses.business.entity.PayInfo;
import com.glasses.business.event.BussEvent;
import com.glasses.business.http.NetUtils;
import com.glasses.business.pay.com.PayAgent;
import com.glasses.business.pay.com.PayAgentUtils;
import com.glasses.business.pay.com.PayListener;
import com.glasses.business.utils.Tools;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/9
 * Version：1.0
 */

public class OrderCompanyFragment extends AppBaseFragment {
    @InjectView(R.id.backSure)
    TextView backSure;
    @InjectView(R.id.transpCompany)
    EditText transpCompany;
    @InjectView(R.id.transpOrderNum)
    EditText transpOrderNum;
    private PayInfo moneyPay;

    @Override
    public int getRootViewId() {
        return R.layout.fragment_order_compayn;
    }

    @Override
    public void setViews() {
        setTitleTv("邮寄信息");
        if (getArguments() != null) {
            moneyPay = ((PayInfo) getArguments().getSerializable("payMoney"));
        }
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public void onEventMainThread(BussEvent event) {
        if (event.getState() == BussEvent.After_Pay) {
            MyApplication.getInstance().manager.onBackPressed();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public boolean mainTabShow() {
        return false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.backSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backSure:
                String company = transpCompany.getText().toString();
                String orderNum = transpOrderNum.getText().toString();
                if (TextUtils.isEmpty(company)) {
                    ToastUtil.shortShowToast("请先填写快递公司...");
                } else if (TextUtils.isEmpty(orderNum)) {
                    ToastUtil.shortShowToast("请先填写运单号...");
                } else {
                    Tools.showDialog(getActivity());
                    NetUtils.getInstance().backMoney(moneyPay.getId(), company, orderNum, 1, new NetCallBack() {
                        @Override
                        public void onSuccess(String response, String mssage, ResultModel model) {
                            Tools.dismissWaitDialog();
                            ToastUtil.shortShowToast(mssage);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("payMoney", moneyPay);
                            open(new PayFragment(), bundle);
                        }

                        @Override
                        public void onFail(boolean dataError, int errorCode, String mssage) {
                            Tools.dismissWaitDialog();
                            ToastUtil.shortShowToast(mssage);
                        }
                    }, null);
                }
                break;
        }
    }
}
